package com.xmutca.sso.server.web;

import com.xmutca.core.cache.J2Cache;
import com.xmutca.core.common.Result;
import com.xmutca.sso.common.Principal;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

/**
 * TicketController, 为websocket服务做准备
 * @author: 彭伟煌(pengweihuang@xmutca.com)
 * @create: 2016-08-15 22:49
 */
@Controller
@RequestMapping("/ticket")
public class TicketController {

    private final static String CACHE_KEY = "auth-ticket:%s";
    private final static int TIME_OUT = 120;

    /**
     * 登陆成功发布ticket
     * @return
     */
    @ResponseBody
    @RequestMapping
    public Result<String> createTicket() {
        Principal principal = (Principal) SecurityUtils.getSubject().getPrincipal();
        String ticket = UUID.randomUUID().toString();
        J2Cache.getInstance().set(String.format(CACHE_KEY, ticket), principal.getId(), TIME_OUT);
        return new Result<>(Result.Status.SUCCESS, ticket);
    }
}
